package com.lab;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JTabbedPane;


public class progIT extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private List<Sity> listOfSities = new ArrayList<Sity>();
	private Vector<Edge> newedge=new Vector<Edge>();
	private File file;	 
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					progIT frame = new progIT();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public progIT() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(0, 0, 388, 355);
		setSize(963, 705);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane, BorderLayout.CENTER);

		//���������� ����� ��� ����
		ReadData asd= new ReadData();
		asd.readData();
		listOfSities =asd.getListOfSities();
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("Карта України", null, panel, null);
		panel.paintComponents(null);
		panel.setLayout(null);
		
		CPanel cpan =new CPanel();
		cpan.setBackground(Color.black);
		// set opaque to false - background not drawn
		cpan.setOpaque(false);
		cpan.setBounds(0, 0, 938, 653);
		Kruskal one =new Kruskal();
		
		//���� ����� � �������
		JFileChooser fileopen = new JFileChooser();     
		fileopen.setDialogTitle("Відкрити файл зі шляхами");
        int ret = fileopen.showDialog(null, "Відкрити файл");                
        if (ret == JFileChooser.APPROVE_OPTION) {
            file = fileopen.getSelectedFile();
           // label.setText(file.getName());
        } else if (JFileChooser.CANCEL_SELECTION != null) {
       	 System.out.println("Skasovano");
       	 file= new File("C:\\Users\\MUSTANG\\Desktop\\міста3.txt");
       	 }
        
		one.readInGraphData(file.getAbsolutePath());
		one.performKruskal();
		newedge=one.getNewEdges();
		cpan.setList(newedge,listOfSities);
		//��������� ������������ �����
		panel.add(cpan);
		
		Circle bottomPanel = new Circle();
		//������� ������� ���� � ���������
		bottomPanel.setForeground(Color.green);
		bottomPanel.setBackground(Color.cyan);
		// and it will show this time, because opaque is true
		bottomPanel.setOpaque(false);
		bottomPanel.setBounds(0, 0, 938, 653);
		// ��������� ������
		panel.add(bottomPanel);
		
		CirclePanel pan =new CirclePanel();
		pan.setForeground(Color.blue);
		// background should be black, except it's not opaque, so 
		// background will not be drawn
		pan.setBackground(Color.black);
		// set opaque to false - background not drawn
		pan.setOpaque(true);
		pan.setBounds(0, 0, 938, 653);
		//������� ����
		panel.add(pan);
	}
}


class CirclePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	 private Image image = null;
	
	// This is Swing, so override paint*Component* - not paint
    protected void paintComponent(Graphics g) {
        // call super.paintComponent to get default Swing 
        super.paintComponent(g);
        //��������� ����
       try {
			image = ImageIO.read(new File("D:\\map2.jpg"));
			} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			}
        g.drawImage(image, 0, 0, null); 
             
    }
}


class Circle extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// This is Swing, so override paint*Component* - not paint
    protected void paintComponent(Graphics g) {
        // call super.paintComponent to get default Swing 
        // painting behavior (opaque honored, etc.)
        super.paintComponent(g); //��������� ������
       
        g.drawLine(115, 183, 187, 123);//lviv-luck
        g.drawLine(229, 143, 187, 123);//rivne-luck
        g.drawLine(115, 183, 229, 143);//lviv-rivne
        g.drawLine(115, 183, 153, 229);//lviv-ternopil
        g.drawLine(115, 183, 130, 277);//lviv=ivano-frankivsk
        g.drawLine(115, 183, 12, 287);//lviv-uzhgorod
        g.drawLine(12, 287, 130, 277);//uzhgorod=ivano-frankivsk
        g.drawLine(130, 277, 197, 320);//ivano-frankivsk=chernivci
        g.drawLine(130, 277, 153, 229);//ivano-frankivsk=ternopil
        g.drawLine(153, 229, 197, 320);//ternopil=chernivci
        g.drawLine(153, 229, 229, 143);//ternopil=rivne
        g.drawLine(153, 229, 255, 231);//ternopil=hmelnutskuj
        g.drawLine(229, 143, 255, 231);//rivne=hmelnutskuj
        g.drawLine(229, 143, 332, 182);//rivne=zhutomur
        g.drawLine(197, 320, 255, 231);//chernivci=hmelnutskuj
        g.drawLine(197, 320, 340, 262);//chernivci=vinnucja
        g.drawLine(255, 231, 340, 262);//hmelnutskuj=vinnucja
        g.drawLine(255, 231, 332, 182);//hmelnutskuj=zhutomur
        g.drawLine(332, 182, 340, 262);//zhutomur=vinnucja
        g.drawLine(332, 182, 413, 138);//zhutomur=kyiv
        g.drawLine(340, 262, 483, 251);//vinnucja=cherkasu
        g.drawLine(340, 262, 548, 325);//vinnucja=kirovograd
        g.drawLine(340, 262, 456, 492);//vinnucja=odesa
        g.drawLine(413, 138, 463, 64);//kyiv=chernigiv
        g.drawLine(413, 138, 568, 202);//kyiv=poltava
        g.drawLine(413, 138, 483, 251);//kyiv=cherkasu
        g.drawLine(463, 64, 654, 106);//chernigiv=symu
        g.drawLine(463, 64, 568, 202);//chernigiv=poltava
        g.drawLine(654, 106, 568, 202);//symu=poltava
        g.drawLine(654, 106, 742, 189);//symu=harkiv
        g.drawLine(568, 202, 742, 189);//poltava=harkiv
        g.drawLine(568, 202, 669, 324);//poltava=dnipropetrovsk
        g.drawLine(568, 202, 548, 325);//poltava=kirovograd
        g.drawLine(568, 202, 483, 251);//poltava=cherkasu
        g.drawLine(483, 251, 548, 325);//cherkasu=kirovograd
        g.drawLine(742, 189, 901, 277);//harkiv=lugansk
        g.drawLine(742, 189, 815, 344);//harkiv=donetsk
        g.drawLine(742, 189, 669, 324);//harkiv=dnipropetrovsk
        g.drawLine(901, 277, 815, 344);//lugansk=donetsk
        g.drawLine(815, 344, 686, 362);//donetsk=zaporizhzhja
        g.drawLine(815, 344, 669, 324);//donetsk=dnipropetrovsk
        g.drawLine(548, 325, 669, 324);//kirovograd=dnipropetrovsk
        g.drawLine(548, 325, 497, 459);//kirovograd=mukolajiv
        g.drawLine(548, 325, 456, 492);//kirovograd=odesa
        g.drawLine(669, 324, 686, 362);//dnipropetrovsk=zaporizhzhja
        g.drawLine(669, 324, 545, 468);//dnipropetrovsk=herson
        g.drawLine(669, 324, 497, 459);//dnipropetrovsk=mukolajiv
        g.drawLine(686, 362, 545, 468);//zaporizhzhja=herson
        g.drawLine(456, 492, 497, 459);//odesa=mukolajiv
        g.drawLine(497, 459, 545, 468);//mukolajiv=herson
        g.drawLine(545, 468, 634, 593);//herson=cimferopol
      }
  }


class CPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Vector<Edge> list1 = new Vector<Edge>();//������ �����
	private List<Sity> list2 = new ArrayList<Sity>();//������ ��������� ���
	
	void  setList(Vector<Edge> l1, List<Sity> l2 ){
		this.list1=l1;
		this.list2=l2;
	}
		//��������� ��������� �����
	 protected void paintComponent(Graphics g) {
       super.paintComponent(g);
       Graphics2D g2 = (Graphics2D) g;
       g2.setStroke(new BasicStroke(3));//��������� ������ �?
       int f,s;//���� ��-��
       int sx,sy;//�������� �����
       for(int i=0;i<list1.size();i++){
    	   f=list1.elementAt(i).getFirstSity();
    	   s=list1.elementAt(i).getSecondSity();
    	   g.setColor(Color.MAGENTA);
    	   g.drawLine(list2.get(f).getCordXofSity(), list2.get(f).getCordYofSity(),list2.get(s).getCordXofSity(), list2.get(s).getCordYofSity());
    	  //���������� ��������
    	   sx=(list2.get(f).getCordXofSity()+list2.get(s).getCordXofSity())/2;
    	   sy=(list2.get(f).getCordYofSity()+list2.get(s).getCordYofSity())/2;
    	   //���� ������(������� �����)
    	   g.setColor(Color.black);
    	   g.drawString(""+list1.elementAt(i).cost,sx,sy);
       }
       
    }
}